package com.zking.frame.dhy;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.zking.db.BaseDao;
import com.zking.entityClass.BookInfo;
import com.zking.entityClass.DaySell;
import com.zking.utils.StringUtils;

import sun.net.www.content.image.jpeg;

/**
 * 日常销售面板
 * 
 * @author longl
 *
 */
public class DaySave extends JDialog {
	{
		// 调用美化包
		BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
		try {
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 北部面板
	private JPanel north_jp;
	private JButton list_jb;// 挂单按钮
	private JButton order_jb;// 取单按钮

	// 中部面板
	private JPanel center_jp;
	private DefaultTableModel contentArea;
	private JTable table;
	private String[][] data;
	private String[] header = new String[] { "书名", "库存数量", "定价", "折扣", "折扣后价格", "销售数量", "小计" ,"日期"};
	// public List<Users> userlist = new ArrayList<Users>();
	private int size;
	private int start;
	private int end;

	// 南部面板
	private JPanel south_jp;

	private JLabel jl_booknumber;// 书号标签
	JTextField jtx_input = null;// 输入框
	private JButton jb_query;// 查询
	private JLabel jl_null;// 空的组件
	private JButton SellOut_jb;// 售出商品按钮

	BaseDao base = new BaseDao();
	private Color blue = new Color(0x9FB6CD);// 颜色
	private String pattern = "yyyy-MM-dd";

	public DaySave() {

		setLayout(new BorderLayout());

		// 北部面板
		north_jp = new JPanel(new FlowLayout(FlowLayout.LEADING, 300, 15));
		north_jp.setBackground(blue);

		list_jb = new JButton("挂单");
		list_jb.setFont(new Font("楷体", Font.BOLD, 20));
		list_jb.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(DaySave.this, "没有销售记录可以挂起", "提示", JOptionPane.ERROR_MESSAGE);

			}
		});

		order_jb = new JButton("取单");
		order_jb.setFont(new Font("楷体", Font.BOLD, 20));
		order_jb.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				new OrderFrame(DaySave.this);

			}
		});

		// north_jp.add(list_jb);
		// north_jp.add(order_jb);

		// 中部面板
		center_jp = new JPanel(new BorderLayout());

		// 表格

		contentArea = new DefaultTableModel(header, 0);
		table = new JTable(contentArea);
		JScrollPane sp_center_right = new JScrollPane(table);
		JTableHeader head = table.getTableHeader();
		head.setPreferredSize(new Dimension(head.getWidth(), 35));
		head.setFont(new Font("楷体", Font.BOLD, 18));
		table.setFont(new Font("宋体", Font.PLAIN, 20));

		sp_center_right.getViewport().setOpaque(false);
		sp_center_right.setBackground(Color.WHITE);
		center_jp.add(sp_center_right);

		// 南部面板
		south_jp = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 10));
		south_jp.setBackground(blue);

		jl_booknumber = new JLabel("书号：");
		jl_booknumber.setFont(new Font("楷体", Font.BOLD, 25));

		jtx_input = new JTextField();
		jtx_input.setFont(new Font("楷体", Font.BOLD, 25));
		jtx_input.setPreferredSize(new Dimension(150, 30));

		jb_query = new JButton("查询");
		jb_query.setFont(new Font("楷体", Font.BOLD, 25));

		jb_query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookSearch bs = new BookSearch(false, DaySave.this, false);

			}
		});
		jl_null = new JLabel("                                                   ");
		SellOut_jb = new JButton("售出商品");
		SellOut_jb.setFont(new Font("楷体", Font.BOLD, 25));
		SellOut_jb.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				if (jtx_input.getText().equals("") || jtx_input.getText().length() == 0) {
					JOptionPane.showMessageDialog(DaySave.this, "选择售出书籍的书号", "error", JOptionPane.ERROR_MESSAGE);
					return;
				}

				new sellFrame(DaySave.this);
			}
		});

		south_jp.add(jl_booknumber);
		south_jp.add(jtx_input);
		south_jp.add(jb_query);
		south_jp.add(jl_null);
		south_jp.add(SellOut_jb);

		add(BorderLayout.NORTH, north_jp);
		add(BorderLayout.CENTER, center_jp);
		add(BorderLayout.SOUTH, south_jp);

		allsql();
		this.setBackground(Color.blue);
		setTitle("日常销售");
		setSize(1025, 685);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(this);
		setVisible(true);

	}

	public static void main(String[] args) {

		new DaySave();
	}

	public void allsql() {

		contentArea.setDataVector(null, header);// 清空面板
		String sqlall = "SELECT * FROM daysell ";
		try {
			List<DaySell> daySells = base.findList(DaySell.class, sqlall);
			for (int i = 0; daySells != null && i < daySells.size(); i++) {
				// "书名","库存数量","定价","折扣","折扣后价格","销售数量","小计"
				DaySell daySell = daySells.get(i);
				Vector<Object> rowData = new Vector<>();
				rowData.add(daySell.getBookName());
				rowData.add(daySell.getCountNums());
				rowData.add(daySell.getPricing());
				rowData.add(daySell.getDiscount());
				rowData.add(daySell.getDiscountPrice());
				rowData.add(daySell.getSalesNums());
				rowData.add(daySell.getSubtotal());
				rowData.add(daySell.getListnumber());
				
				contentArea.addRow(rowData);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	public void insertsql(int j, String client, int number) {
		String sql = "SELECT * FROM bookinfo WHERE id=?";

		try {
			List<BookInfo> daySells = base.findList(BookInfo.class, sql, j);
			double price;
			double discount;
			double subtotal;
			int inventoryNums;
			for (int i = 0; daySells != null && i < daySells.size(); i++) {
				// "书名","库存数量","定价","折扣","折扣后价格","销售数量","小计"
				BookInfo daySell = daySells.get(i);
				Vector<Object> rowData = new Vector<>();
				rowData.add(daySell.getBookName());
				rowData.add(daySell.getInventoryNums());
				inventoryNums = daySell.getInventoryNums();
				rowData.add(daySell.getPrice());
				if (client.equals("会员")) {
					rowData.add(0.85);
					discount = 0.85;
					rowData.add(daySell.getVipPrice());
					price = daySell.getVipPrice();
				} else {
					rowData.add(0);
					discount = 0;
					rowData.add(daySell.getPrice());
					price = daySell.getPrice();
				}
				rowData.add(number);
				rowData.add(number * price);
				subtotal = number * price;
				rowData.add(StringUtils.dateToString(new Date(), pattern));
				contentArea.addRow(rowData);

				Object[] params = { daySell.getBookName(), daySell.getInventoryNums(), daySell.getPrice(), discount,
						price, number, subtotal, j, StringUtils.dateToString(new Date(), pattern), null };
				String sql1 = "insert into daysell values(?,?,?,?,?,?,?,?,?,?)";
				base.update(sql1, params);

				String sql2 = "update bookinfo set inventoryNums=? where id=?";
				base.update(sql2, inventoryNums - number, j);

			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	/**
	 * 售出窗口
	 * 
	 * @author longl
	 *
	 */
	class sellFrame extends JDialog {
		// 北部面板
		private JPanel north_jp;
		private JLabel jl_number;
		private JComboBox<Integer> jcb_number;
		private JComboBox<String> jcb_client;
		// 中部面板
		private JPanel center_jp;
		private JButton confirm_jb;
		private JButton return_jb;

		public sellFrame(DaySave daySave) {
			super(daySave, "售出", false);

			setLayout(new BorderLayout());
			// 北部面板
			north_jp = new JPanel(new FlowLayout());
			jl_number = new JLabel("请选择数量：");
			jl_number.setFont(new Font("楷体", Font.BOLD, 20));

			jcb_number = new JComboBox<Integer>();
			jcb_number.setFont(new Font("楷体", Font.BOLD, 28));
			jcb_number.setPreferredSize(new Dimension(100, 30));
			int j = Integer.parseInt(daySave.jtx_input.getText());
			String sql = "SELECT * FROM bookinfo WHERE id=?";
			try {
				List<BookInfo> book = base.findList(BookInfo.class, sql, j);
				int max = book.get(0).getInventoryNums();
				for (int i = 1; i <= max; i++) {
					jcb_number.addItem(i);

				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			jcb_client = new JComboBox<String>();
			jcb_client.setFont(new Font("楷体", Font.BOLD, 28));
			jcb_client.setPreferredSize(new Dimension(200, 30));
			String[] client = { "会员", "普通用户" };
			for (int i = 0; i < client.length; i++) {
				jcb_client.addItem(client[i]);

			}

			north_jp.add(jl_number);
			north_jp.add(jcb_number);
			north_jp.add(jcb_client);

			// 中部面板
			center_jp = new JPanel(new FlowLayout());

			confirm_jb = new JButton("确定");
			confirm_jb.setFont(new Font("楷体", Font.BOLD, 20));
			confirm_jb.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					insertsql(j, jcb_client.getSelectedItem().toString(), (int) jcb_number.getSelectedItem());
					dispose();
				}
			});

			return_jb = new JButton("返回");
			return_jb.setFont(new Font("楷体", Font.BOLD, 20));
			return_jb.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					dispose();

				}
			});

			center_jp.add(return_jb);
			center_jp.add(confirm_jb);

			add(BorderLayout.NORTH, north_jp);
			add(BorderLayout.CENTER, center_jp);

			this.setBackground(Color.blue);
			setSize(500, 200);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setLocationRelativeTo(this);
			setVisible(true);

		}

	}

	/**
	 * 取单子窗口
	 * 
	 * @param args
	 */

	class OrderFrame extends JDialog {

		// 北部面板
		private JPanel north_jp;
		private DefaultTableModel contentArea;
		private JTable table;
		private String[][] data;
		private String[] header = new String[] { "序号", "单号", "合计数量", "日期", };
		// public List<Users> userlist = new ArrayList<Users>();
		private int size;
		private int start;
		private int end;
		private Border border = BorderFactory.createMatteBorder(0, 0, 3, 2, Color.gray);
		// 中部面板
		private JPanel center_jp;
		private JButton extract_jb;// 提取按钮

		private Color blue = new Color(0x9FB6CD);// 颜色

		public OrderFrame(JDialog owner) {
			super(owner, "取单窗口", true);
			setLayout(new BorderLayout());

			// 北部面板
			north_jp = new JPanel(new BorderLayout());

			// 表格
			contentArea = new DefaultTableModel(header, 0);
			table = new JTable(contentArea);
			JScrollPane sp_center_right = new JScrollPane(table);
			table.getColumnModel().getColumn(3).setPreferredWidth(200);
			JTableHeader head = table.getTableHeader();
			head.setPreferredSize(new Dimension(head.getWidth(), 35));
			head.setFont(new Font("楷体", Font.BOLD, 18));
			table.setFont(new Font("宋体", Font.PLAIN, 20));
			sp_center_right.getViewport().setOpaque(false);
			sp_center_right.setBackground(Color.white);
			north_jp.add(sp_center_right);

			// 中部面板
			center_jp = new JPanel(new FlowLayout(FlowLayout.LEADING, 180, 15));
			center_jp.setBackground(blue);
			extract_jb = new JButton("提取所选");
			extract_jb.setFont(new Font("楷体", Font.BOLD, 25));
			extract_jb.setBorder(border);
			extract_jb.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					dispose();

				}
			});
			center_jp.add(extract_jb);

			add(BorderLayout.NORTH, north_jp);
			add(BorderLayout.SOUTH, center_jp);
			setSize(500, 543);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setLocationRelativeTo(this);
			setVisible(true);

		}

	}
}
